﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.Platform
{
    /// <summary>
    /// 数据字典
    /// </summary>
    public class DataDictService : PlatformAppService, IDataDictService
    {
        private readonly IRepository<DataDict, Guid> _dataDictRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataDictRepository"></param>
        public DataDictService(IRepository<DataDict, Guid> dataDictRepository)
        {
            _dataDictRepository = dataDictRepository;
        }

        /// <summary>
        /// 获取字典分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<DataDictDto>> GetListAsync(DataDictInput input)
        {
            var a = CurrentUser;
            var iQuery = await _dataDictRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.CatgeCodg) && input.CatgeCodg != "00", x => x.CatgeCodg == input.CatgeCodg);
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), d => d.DataDictCode.Contains(input.Filter)
                || d.DataDictName.Contains(input.Filter));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DataDict>, List<DataDictDto>>(iQuery.ToList());

                return new PagedResultDto<DataDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<DataDict>, List<DataDictDto>>(items.ToList());

                return new PagedResultDto<DataDictDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 添加字典数据
        /// </summary>
        /// <returns></returns>
        public async Task<DataDictDto> PostAsync(DataDictCreateDto input)
        {
            var dataDictionary = ObjectMapper.Map<DataDictCreateDto, DataDict>(input);
            dataDictionary = await _dataDictRepository.InsertAsync(dataDictionary);
            var itemDto = ObjectMapper.Map<DataDict, DataDictDto>(dataDictionary);

            return itemDto;
        }

        /// <summary>
        /// 修改字典数据
        /// </summary>
        /// <returns></returns>
        public async Task<DataDictDto> PutAsync(Guid id, DataDictUpdateDto input)
        {
            var dataDictionary = await _dataDictRepository.GetAsync(id);
            ObjectMapper.Map(input, dataDictionary);
            dataDictionary = await _dataDictRepository.UpdateAsync(dataDictionary);
            var itemDto = ObjectMapper.Map<DataDict, DataDictDto>(dataDictionary);

            return itemDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid id)
        {
            await _dataDictRepository.DeleteAsync(id);
        }
    }
}
